﻿// Decompiled with JetBrains decompiler
// Type: MCM.Implementation.PerSave.FluentPerSaveSettingsContainer
// Assembly: MCMv5, Version=5.10.2.0, Culture=neutral, PublicKeyToken=null
// MVID: 7AB326B9-B2B4-41C2-9177-68E944071101
// Assembly location: D:\steam\steamapps\workshop\content\261550\2859238197\bin\Win64_Shipping_Client\MCMv5.dll

using BUTR.DependencyInjection;
using MCM.Abstractions;
using MCM.Abstractions.Base;
using MCM.Abstractions.Base.PerSave;
using MCM.Abstractions.GameFeatures;
using MCM.Abstractions.PerSave;
using System;
using System.Collections.Generic;

#nullable enable
namespace MCM.Implementation.PerSave
{
  internal sealed class FluentPerSaveSettingsContainer : 
    BaseSettingsContainer<FluentPerSaveSettings>,
    IFluentPerSaveSettingsContainer,
    IPerSaveSettingsContainer,
    ISettingsContainer,
    ISettingsContainerHasSettingsDefinitions,
    ISettingsContainerCanOverride,
    ISettingsContainerCanReset,
    ISettingsContainerPresets,
    ISettingsContainerHasUnavailable,
    ISettingsContainerHasSettingsPack,
    ISettingsContainerCanInvalidateCache
  {
    private readonly IGameEventListener _gameEventListener;

    public event Action? InstanceCacheInvalidated;

    public FluentPerSaveSettingsContainer(IGameEventListener gameEventListener)
    {
      this._gameEventListener = gameEventListener;
      this._gameEventListener.GameStarted += new Action(this.GameStarted);
      this._gameEventListener.GameEnded += new Action(this.GameEnded);
    }

    public void Register(FluentPerSaveSettings settings) => this.RegisterSettings(settings);

    public void Unregister(FluentPerSaveSettings settings)
    {
      if (!this.LoadedSettings.ContainsKey(settings.Id))
        return;
      this.LoadedSettings.Remove(settings.Id);
    }

    protected override void RegisterSettings(FluentPerSaveSettings? perSaveSettings)
    {
      if (GenericServiceProvider.GameScopeServiceProvider == null)
        return;
      IPerSaveSettingsProvider service = GenericServiceProvider.GetService<IPerSaveSettingsProvider>();
      if (service == null || perSaveSettings == null)
        return;
      this.LoadedSettings.Add(perSaveSettings.Id, perSaveSettings);
      service.LoadSettings((PerSaveSettings) perSaveSettings);
      perSaveSettings.OnPropertyChanged("LOADING_COMPLETE");
    }

    public override bool SaveSettings(BaseSettings settings)
    {
      if (GenericServiceProvider.GameScopeServiceProvider == null)
        return false;
      IPerSaveSettingsProvider service = GenericServiceProvider.GetService<IPerSaveSettingsProvider>();
      if (service == null || !(settings is PerSaveSettings perSaveSettings) || !this.LoadedSettings.ContainsKey(perSaveSettings.Id))
        return false;
      service.SaveSettings(perSaveSettings);
      settings.OnPropertyChanged("SAVE_TRIGGERED");
      return true;
    }

    public void LoadSettings()
    {
    }

    public void GameStarted() => this.LoadedSettings.Clear();

    public void GameEnded() => this.LoadedSettings.Clear();

    public IEnumerable<UnavailableSetting> GetUnavailableSettings()
    {
      return (IEnumerable<UnavailableSetting>) Array.Empty<UnavailableSetting>();
    }
  }
}
